/*
 * Copyright (C) 2004, Velocityme Partnership
 * IssueActions.java
 *
 * Created on 12 September 2004, 01:19
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.www.actionforms.nodeactionforms.IssueValueActionForm;
import com.velocityme.www.dto.IntegerString;
import com.velocityme.www.utility.NodeUtil;
import java.util.*;
import javax.servlet.http.*;
import javax.swing.tree.TreeModel;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author  Robert
 */
public class IssueActions extends TaskActions {
    
    /** Creates a new instance of IssueActions */
    public IssueActions() {
    }
    
    public ActionForward show(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        IssuePK issuePK = new IssuePK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("interestedContactableValues", cs.getTaskInterestedContactableValues(key, taskPK));
        session.setAttribute("responsibleContactableValues", cs.getTaskResponsibleContactableValues(key, taskPK));
        session.setAttribute("issueValue", cs.getIssueValue(key, issuePK));
        session.setAttribute("issueHistory", cs.getHistoryValue(key, nodePK));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreate(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        IssuePK issuePK = new IssuePK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("parentNodePK", nodePK);
        Collection priorityIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=10)
            priorityIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("priorityPairs", priorityIntegerStrings);
        Collection completeIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=5)
            completeIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("completePairs", completeIntegerStrings);
        Collection ownerIntegerStrings = new ArrayList();
        TreeModel contactableTreeModel = cs.getContactableValueTree(key);
        NodeUtil.recursiveAddContactable(contactableTreeModel, contactableTreeModel.getRoot(), ownerIntegerStrings, 0);
        session.setAttribute("ownerPairs", ownerIntegerStrings);
        session.setAttribute("taskTypePairs", NodeUtil.getAllTaskTypePairs(key, cs));
        IssueValueActionForm issueValueActionForm = new IssueValueActionForm();
        issueValueActionForm.setOwnerId(key.getPersonPK().getPersonId());
        issueValueActionForm.setPercentageComplete(new Byte((byte)0));
        issueValueActionForm.setIsInterested(Boolean.TRUE);
        issueValueActionForm.setDurationTotalMins(new Integer(0));
        session.setAttribute("days", NodeUtil.getInstance().getDurationDays());
        session.setAttribute("hours", NodeUtil.getInstance().getDurationHours());
        session.setAttribute("minutes", NodeUtil.getInstance().getMinutes());
        session.setAttribute("issueActionForm", issueValueActionForm);
        IssueValue issueValue = new IssueValue();
        issueValue.setTaskValue(new TaskValue());
        issueValue.getTaskValue().setNodeValue(new NodeValue());
        session.setAttribute("issueValue", issueValue);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        IssuePK issuePK = new IssuePK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        IssueValue issueValue = cs.getIssueValue(key, issuePK);
        session.setAttribute("issueValue", issueValue);
        IssueValueActionForm issueValueActionForm = new IssueValueActionForm();
        issueValueActionForm.setName(issueValue.getTaskValue().getNodeValue().getName());
        issueValueActionForm.setDescription(issueValue.getTaskValue().getNodeValue().getDescription());
        issueValueActionForm.setPriority(issueValue.getTaskValue().getPriority());
        issueValueActionForm.setOwnerId(issueValue.getTaskValue().getOwnerContactableValue().getContactableId());
        issueValueActionForm.setTaskTypeId(issueValue.getTaskValue().getTaskStateMachineValue().getTaskStateMachineId());
        issueValueActionForm.setPercentageComplete(issueValue.getTaskValue().getPercentageComplete());
        issueValueActionForm.setDurationTotalMins(issueValue.getTaskValue().getExpectedDuration_min());
        session.setAttribute("days", NodeUtil.getInstance().getDurationDays());
        session.setAttribute("hours", NodeUtil.getInstance().getDurationHours());
        session.setAttribute("minutes", NodeUtil.getInstance().getMinutes());
        session.setAttribute("issueActionForm", issueValueActionForm);
        Collection priorityIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=10)
            priorityIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("priorityPairs", priorityIntegerStrings);
        Collection completeIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=5)
            completeIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("completePairs", completeIntegerStrings);
        Collection ownerIntegerStrings = new ArrayList();
        TreeModel contactableTreeModel = cs.getContactableValueTree(key);
        NodeUtil.recursiveAddContactable(contactableTreeModel, contactableTreeModel.getRoot(), ownerIntegerStrings, 0);
        session.setAttribute("ownerPairs", ownerIntegerStrings);
        Collection taskTypeIntegerStrings = new ArrayList();
        Iterator i = cs.getAllTaskStateMachineValues(key).iterator();
        while(i.hasNext()) {
            TaskStateMachineValue taskStateMachineValue = (TaskStateMachineValue)i.next();
            taskTypeIntegerStrings.add(new IntegerString(taskStateMachineValue.getTaskStateMachineId(), taskStateMachineValue.toString()));
        }
        session.setAttribute("taskTypePairs", taskTypeIntegerStrings);
        session.setAttribute("allContactablePairs", NodeUtil.getAllContactablePairs(key, cs));
        Collection interestedContactableIntegerStrings = new ArrayList();
        i = cs.getTaskInterestedContactableValues(key, taskPK).iterator();
        while(i.hasNext()) {
            ContactableValue contactableValue = (ContactableValue)i.next();
            interestedContactableIntegerStrings.add(new IntegerString(contactableValue.getContactableId(), ContactableValueToString.toString(contactableValue)));
        }
        session.setAttribute("interestedContactablePairs", interestedContactableIntegerStrings);
        Collection responsibleContactableIntegerStrings = new ArrayList();
        i = cs.getTaskResponsibleContactableValues(key, taskPK).iterator();
        while(i.hasNext()) {
            ContactableValue contactableValue = (ContactableValue)i.next();
            responsibleContactableIntegerStrings.add(new IntegerString(contactableValue.getContactableId(), ContactableValueToString.toString(contactableValue)));
        }
        session.setAttribute("responsibleContactablePairs", responsibleContactableIntegerStrings);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK;
        if (request.getParameter("btnSubmit").equals("OK")) {
            IssueValueActionForm issueValueActionForm = (IssueValueActionForm)form;
            IssueValue issueValue = (IssueValue)session.getAttribute("issueValue");
            issueValue.getTaskValue().getNodeValue().setName(issueValueActionForm.getName());
            issueValue.getTaskValue().getNodeValue().setDescription(issueValueActionForm.getDescription());
            issueValue.getTaskValue().setPriority(issueValueActionForm.getPriority());
            ContactableValue ownerContactableValue = new ContactableValue();
            ownerContactableValue.setPrimaryKey(new ContactablePK(issueValueActionForm.getOwnerId()));
            issueValue.getTaskValue().setOwnerContactableValue(ownerContactableValue);
            TaskStateMachineValue taskStateMachineValue = new TaskStateMachineValue();
            taskStateMachineValue.setPrimaryKey(new TaskStateMachinePK(issueValueActionForm.getTaskTypeId()));
            issueValue.getTaskValue().setTaskStateMachineValue(taskStateMachineValue);
            issueValue.getTaskValue().setPercentageComplete(issueValueActionForm.getPercentageComplete());
            issueValue.getTaskValue().setExpectedDuration_min(issueValueActionForm.getDurationTotalMins());
            IssuePK issuePK;
            if (issueValue.issueIdHasBeenSet()) {
                issuePK = issueValue.getPrimaryKey();
                cs.issueEdit(key, issuePK, issueValue, issueValueActionForm.getResponsibleContactablePKs(), issueValueActionForm.getInterestedContactablePKs());
            }
            else {
                NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
                issuePK = cs.issueCreate(key, parentNodePK, issueValue);
                if (((IssueValueActionForm)form).getIsInterested().equals(Boolean.TRUE))
                    cs.taskEnableInterestedContactableStateValue(key, new TaskPK(issuePK.getIssueId()));
            }
            nodePK = new NodePK(issuePK.getIssueId());
        }
        // Cancel simply displays the original node again
        else {
            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
}
